package com.snake.driver;

import com.snake.bean.HfTaobaoShop;
import com.snake.db.Demo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.Arrays;
import java.util.List;

public class TaobaoShopWebDriver {

    private static WebDriver driver;
    private static String baseUrl = "http://taobao.com";
    private static StringBuffer verificationErrors = new StringBuffer();


    public static void main(String[] args) {

        String browserDriverUrl = "E:\\chromedriver\\chromedriver.exe";// 浏览器驱动路径
        //启动chrome浏览器
        System.setProperty("webdriver.chrome.driver", browserDriverUrl);

        //取消 chrome正受到自动测试软件的控制的信息栏
        ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
        options.addArguments("--test-type", "--start-maximized");

        driver = new ChromeDriver(options);
        //driver.manage().window().maximize();// 最大化浏览器
        //driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);//设置操作超时时长，该设置是全局性的，即所有操作都最长等待30s
        //  driver.get(baseUrl);
        Demo taobaoShopLinkDemo = new Demo();
        List<HfTaobaoShop> taobaoShopLink = taobaoShopLinkDemo.getTaobaoShop();
        try {
            Thread.sleep(5000);
            System.out.println("查询到的店铺数==>"+taobaoShopLink.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int n=0;n<taobaoShopLink.size();n++){
            String shopUrl = "";
            int shopId =taobaoShopLink.get(n).getShopId();
            shopUrl = taobaoShopLink.get(n).getShopDomain();
            System.out.println("店铺=====>"+shopUrl);
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            driver.get(shopUrl);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            By chooseButtonBy = By.id("J_SearchTab");
            //点开搜索类型下拉框，将鼠标移动到用户上并选择
            Actions act = new Actions(driver);
            WebElement dropDownElement = driver.findElement(chooseButtonBy);
            act.moveToElement(dropDownElement).perform();


            By selfShopBy = By.cssSelector("#J_SearchTab > ul > li:nth-child(2) > a");
            WebElement selfShopButtonElement = driver.findElement(selfShopBy);
            act.moveToElement(selfShopButtonElement).click().perform();


            By searchInput = By.id("q");
            WebElement searchInputElement = driver.findElement(searchInput);
            act.moveToElement(searchInputElement).sendKeys(searchInputElement, "").perform();


            // searchInputElement.sendKeys("衣");

            By searchButtonBy = By.className("search-button");
            WebElement searchButtonElement = driver.findElement(searchButtonBy);
            act.moveToElement(searchButtonElement).click().perform();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            By totalPageBy = By.className("page-info");
            String totalPageText = driver.findElement(totalPageBy).getText();
            int totalPage = Integer.parseInt(totalPageText.split("/")[1]);
            System.out.println("店铺===>"+shopUrl+"页数为===>"+totalPage);
            for(int i=1;i<totalPage;i++){

                By netPageBy = By.cssSelector("#J_ShopSearchResult > div > div.shop-hesper-bd.grid > div.shop-filter > div.pagination.pagination-mini > a:nth-child(3)");
                WebElement netPageElement = driver.findElement(netPageBy);
                act.moveToElement(netPageElement).click().perform();
                System.out.println("======>这是第"+i+"页");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //下一页
                String nextPageSource = driver.getPageSource();
                //  System.out.println("=============================="+nextPageSource);
                Document document = Jsoup.parse(nextPageSource);
                Elements elements = document.getElementsByClass("detail");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (null != elements && elements.size() > 0) {
                    for (Element element : elements) {
                        Elements elementselementA = element.getElementsByClass("item-name J_TGoldData");
                        if ( elementselementA.size() > 0) {
                            String title =  elementselementA.get(0).text();
                            String linkUrl = elementselementA.get(0).attr("href");
                            System.out.println("商品===>" +linkUrl);
                            Demo saveTaobaoLinkDemo = new Demo();
                            saveTaobaoLinkDemo.saveTaobaoLink(shopId,shopUrl,title,linkUrl);
                        }
                    }
                }else{
                    break;
                }
            }
        }

    }


    public static  void showTaobaoLink(){
        //  By  good =  By.className("item3line1");
        Document document = Jsoup.parse(driver.getPageSource());
        Elements elements = document.getElementsByClass("detail");

        if (null != elements && elements.size() > 0) {
            for (Element element : elements) {
                Elements elementselementA = element.getElementsByClass("item-name J_TGoldData");
                if (null != elementselementA && elementselementA.size() > 0) {
                    System.out.println("商品===>" + elementselementA.get(0).attr("href"));
                }
            }
        }
    }

}
